package com.ruoyi.hotel.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hotel.domain.HlDevice;
import com.ruoyi.hotel.domain.HlRoomDevice;
import com.ruoyi.hotel.domain.HlRoomType;
import com.ruoyi.hotel.domain.dto.HlRoomDeviceDTO;
import com.ruoyi.hotel.domain.pojo.HlRoomDeviceAndJsonPojo;
import com.ruoyi.hotel.domain.pojo.HlRoomDevicePojo;
import com.ruoyi.hotel.mapper.HlDeviceMapper;
import com.ruoyi.hotel.mapper.HlRoomDeviceMapper;
import com.ruoyi.hotel.mapper.HlRoomTypeMapper;
import com.ruoyi.hotel.service.IHlRoomDeviceService;
import com.ruoyi.hotel.service.IHlRoomDeviceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 房间设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-10
 */
@Service
public class HlRoomDeviceServiceImpl implements IHlRoomDeviceService 
{
    @Autowired
    private HlRoomDeviceMapper hlRoomDeviceMapper;

    @Autowired
    private HlRoomTypeMapper hlRoomTypeMapper;

    @Autowired
    private HlDeviceMapper hlDeviceMapper;

    /**
     * 查询房间设备
     * 
     * @param id 房间设备主键
     * @return 房间设备
     */
    @Override
    public HlRoomDevice selectHlRoomDeviceById(Integer id)
    {
        return hlRoomDeviceMapper.selectHlRoomDeviceById(id);
    }

    /**
     * 查询房间设备列表
     * 
     * @param hlRoomDevice 房间设备
     * @return 房间设备
     */
    @Override
    public List<HlRoomDeviceDTO> selectHlRoomDeviceList(HlRoomDevice hlRoomDevice)
    {
        List<HlRoomDevice> hlRoomDevices = hlRoomDeviceMapper.selectHlRoomDeviceList(hlRoomDevice);

        List<HlRoomDeviceDTO> hlRoomDeviceDTOS = new ArrayList<>();

        for(HlRoomDevice roomDevice:hlRoomDevices){

            HlRoomDeviceDTO hlRoomDeviceDTO = new HlRoomDeviceDTO();

            BeanUtils.copyProperties(roomDevice,hlRoomDeviceDTO);

            HlRoomType hlRoomType = hlRoomTypeMapper.selectHlRoomTypeById(roomDevice.getRoomTypeId());
            HlRoomType roomType = new HlRoomType();
            roomType.setName(hlRoomType.getName());

            hlRoomDeviceDTO.setRoomType(roomType);
            hlRoomDeviceDTOS.add(hlRoomDeviceDTO);

        }

        return hlRoomDeviceDTOS;
    }


    /**
     * 查询房间类型列表根据房间类型名
     *
     * @param roomTypeName 房间设备
     * @return 房间设备
     */
    @Override
    public AjaxResult selectHlRoomDeviceByRoomTypeName(String roomTypeName)
    {



        HlRoomType hlRoomType = hlRoomTypeMapper.selectHlRoomTypeByName(roomTypeName);

        if(StringUtils.isBlank(hlRoomType.getName())){
            return error("查不到该房间的数据，请确认有该房间");
        }



        HlRoomDevice hlRoomDevice = hlRoomDeviceMapper.selectHlRoomDeviceByRoomTypeNameId(hlRoomType.getId());

        if(Objects.isNull(hlRoomDevice)){
            return error("查不到该房间的数据，请确认有该房间设备");
        }

        List<HlRoomDeviceDTO> list = new ArrayList<>();
        HlRoomDeviceDTO hlRoomDeviceDTO = new HlRoomDeviceDTO();
        BeanUtils.copyProperties(hlRoomDevice,hlRoomDeviceDTO);
        hlRoomDeviceDTO.setRoomType(hlRoomType);
        list.add(hlRoomDeviceDTO);
        return success("查询成功",list);
    }


    /**
     * 新增房间类型AndJson
     *
     * @param hlRoomDevicePojo 房间类型
     * @return 结果
     */
    public AjaxResult addRoomTypeAndJson(HlRoomDevicePojo hlRoomDevicePojo){

        if(Objects.isNull(hlRoomDevicePojo)){
            return error("请填写数据再提交");
        }
        if(StringUtils.isBlank(hlRoomDevicePojo.getRoomTypeName())){
            return error("请选择房间类型再提交");
        }
        List<HlRoomDeviceAndJsonPojo> roomDeviceJson = hlRoomDevicePojo.getRoomDeviceJson();
        for(HlRoomDeviceAndJsonPojo pojo:roomDeviceJson){
            if(StringUtils.isBlank(pojo.getName())){
                return error("数据填写不完整，请输入完整后提交，如不需要该设备，请移除这条设备信息");
            }
            if(StringUtils.isBlank(pojo.getCount())){
                return error("数据填写不完整，请输入完整后提交，如不需要该设备，请删除这条设备信息再提交");
            }
        }

//        这里j 必须是 j= i+1  ,如果是 int j = 1 会出现索引检验bug
        for (int i = 0; i < roomDeviceJson.size(); i++) {
            for (int j = i+1; j <roomDeviceJson.size() ; j++) {
                if(roomDeviceJson.get(i).getName().equals(roomDeviceJson.get(j).getName())){
                    return error("[ 设备名："+roomDeviceJson.get(j).getName()+" ] 数据重复，请删关闭此弹出页面框，重新新增数据");
                }
            }
        }



        HlRoomType hlRoomType = hlRoomTypeMapper.selectHlRoomTypeByName(hlRoomDevicePojo.getRoomTypeName());
        Integer roomTypeId = hlRoomType.getId();
        HlRoomDevice roomDevice = hlRoomDeviceMapper.selectHlRoomDeviceByRoomTypeId(roomTypeId);
        if(!Objects.isNull(roomDevice)){
            return error("新增失败，已经有该房间设备，如需新增请删除该房间设备数据，再操作该房间的新增");
        }

        String jsonString = JSON.toJSONString(roomDeviceJson);
        String username = getUsername();

        HlRoomDevice hlRoomDevice = new HlRoomDevice();
        hlRoomDevice.setRoomTypeId(roomTypeId);
        hlRoomDevice.setDeviceNameAndCount(jsonString);
        hlRoomDevice.setState(1);
        hlRoomDevice.setCreateBy(username);
        hlRoomDevice.setUpdateBy(username);
        hlRoomDevice.setCreateTime(DateUtils.getNowDate());
        hlRoomDevice.setUpdateTime(DateUtils.getNowDate());

        int i = hlRoomDeviceMapper.insertHlRoomDevice(hlRoomDevice);
        if(i < 1){
            return error("新增失败，请联系管理员 或 重试");
        }


        return success("新增成功");

    }

    @Override
    public AjaxResult updateRoomDevice(HlRoomDevicePojo hlRoomDevicePojo) {

        List<HlRoomDeviceAndJsonPojo> roomDeviceJson = hlRoomDevicePojo.getRoomDeviceJson();
        for(HlRoomDeviceAndJsonPojo pojo:roomDeviceJson){
            if(StringUtils.isBlank(pojo.getName())){
                return error("数据填写不完整，请输入完整后提交，如不需要该设备，请移除这条设备信息");
            }
            if(StringUtils.isBlank(pojo.getCount())){
                return error("数据填写不完整，请输入完整后提交，如不需要该设备，请删除这条设备信息再提交");
            }
        }

//        这里j 必须是 j= i+1  ,如果是 int j = 1 会出现索引检验bug
        for (int i = 0; i < roomDeviceJson.size(); i++) {
            for (int j = i+1; j <roomDeviceJson.size() ; j++) {
                if(roomDeviceJson.get(i).getName().equals(roomDeviceJson.get(j).getName())){
                    return error("[ 设备名："+roomDeviceJson.get(j).getName()+" ] 数据重复，请删关闭此弹出页面框，重新修改数据");
                }
            }
        }



        HlRoomType hlRoomType = hlRoomTypeMapper.selectHlRoomTypeByName(hlRoomDevicePojo.getRoomTypeName());
        Integer roomTypeId = hlRoomType.getId();
        String username = getUsername();




        String jsonString = JSON.toJSONString(roomDeviceJson);

        HlRoomDevice hlRoomDevice = new HlRoomDevice();
        hlRoomDevice.setRoomTypeId(roomTypeId);
        hlRoomDevice.setDeviceNameAndCount(jsonString);
        hlRoomDevice.setUpdateBy(username);
        hlRoomDevice.setUpdateTime(DateUtils.getNowDate());

        int i = hlRoomDeviceMapper.updateHlRoomDeviceByRoomTypeIdAndStateIsOne(hlRoomDevice);
        if(i < 1){
            return error("修改失败，请联系管理员 或 重试");
        }


        return success("修改成功");
    }



    /**
     * 批量删除房间设备
     * 
     * @param ids 需要删除的房间设备主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteHlRoomDeviceByIds(Integer[] ids)
    {
        if(ids.length == 0){
            return error("未拿到前端传输过来的id数据,请检查前端");
        }
        String username = getUsername();
        int length = ids.length;
        int i = hlRoomDeviceMapper.updateHlRoomDeviceStateIsZeroByIds(username,ids);
        if(i == 0){
            return error("删除失败，请联系管理员");
        }
        if(i < length){
            return success("部分删除成功，未删除的数据有："+(length - i) +"条数据，此次删除了"+i+"条数据");
        }
        return success("删除成功");
    }

    /**
     * 删除房间设备信息
     * 
     * @param id 房间设备主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteHlRoomDeviceById(Integer id)
    {

        if(Objects.isNull(id)){
            return error("未拿到前端传输过来的id数据,请检查前端");
        }

        String username = getUsername();
        HlRoomDevice roomDevice = new HlRoomDevice();

        roomDevice.setId(id);
        roomDevice.setUpdateBy(username);
        roomDevice.setUpdateTime(DateUtils.getNowDate());

        int j = hlRoomDeviceMapper.updateHlRoomDeviceStateIsZeroById(roomDevice);
        if(j < 1){
            return error("删除失败，请联系管理员 或 重试");
        }

        return success("删除成功");
    }
}
